package com.yellowpage.wf.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.wf.WorkPanelGenerator;
import com.yellowpage.wf.data.Resource;
import com.yellowpage.wf.data.Workflow;
import com.yellowpage.wf.data.WorkflowInstance;
import com.yellowpage.wf.data.WorkflowNodeInstance;
import com.yellowpage.wf.data.WorkflowNode;
import com.yellowpage.wf.data.Worklist;
import com.yellowpage.wf.data.WorkflowPath;
import com.yellowpage.wf.data.Context;
import com.yellowpage.wf.data.ContextInstance;
import com.yellowpage.wf.data.NodeContextInstance;
import com.yellowpage.wf.data.NodeContext;
import com.yellowpage.wf.data.Attachment;
import com.yellowpage.wf.data.NodeAttachment;
import com.yellowpage.wf.data.NodeAttachmentInstance;
import com.yellowpage.wf.data.AttachmentInstance;

import com.yellowpage.wf.service.NodeTemplateService;
import com.yellowpage.wf.service.ResourceService;
import com.yellowpage.wf.service.WorkflowInstanceService;
import com.yellowpage.wf.service.WorkflowNodeService;
import com.yellowpage.wf.service.WorkflowService;
import com.yellowpage.wf.service.WorklistService;
import com.yellowpage.wf.service.WorkflowPathService;
import com.yellowpage.wf.service.WorkflowNodeInstanceService;
import com.yellowpage.wf.service.ContextInstanceService;
import com.yellowpage.wf.service.NodeContextInstanceService;
import com.yellowpage.wf.service.AttachmentService;
import com.yellowpage.wf.service.AttachmentInstanceService;
import com.yellowpage.wf.service.NodeAttachmentService;
import com.yellowpage.wf.service.NodeAttachmentInstanceService;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class WorklistAction extends BaseAction {

	public String RESULT_QUERY = "success";
	public String RESULT_DETAIL = "detail";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	public String RESULT_WATCH = "watch";
	public String RESULT_ATTACHMENT_ADD = "attachment_add";
	public String RESULT_TO_UPLOAD = "to_upload";
	public String RESULT_TO_NODE_UPLOAD = "to_node_upload";
	public String RESULT_NODE_HIS = "node_his";
	public String RESULT_HIS = "his";
	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<Map> resultList = new ArrayList<Map>();
	private List<Map> workList = new ArrayList<Map>();
	private List<Map> workhistoryList = new ArrayList<Map>();
	private List<Map> nodeList = new ArrayList<Map>();
	private List<Map> workflowNodeInstanceMapList;
	private List<Map> contextList = new ArrayList<Map>();
	private List<Map> nodeContextList = new ArrayList<Map>();
	private List<Map> attachmentList = new ArrayList<Map>();
	private List<Map> nodeAttachmentList = new ArrayList<Map>();
	
	private File file; 
	private String fileFileName; 
	
	private Constants constants;
	private Worklist worklist;
	private Workflow workflow;
	private WorkflowPath workflowPath;
	private WorkflowNode workflowNode;
	private WorkflowInstance workflowInstance;
	private WorkflowNodeInstance workflowNodeInstance;
	private Context context;
	private ContextInstance contextInstance;
	private NodeContextInstance nodeContextInstance;
	private NodeContext nodeContext;
	private Attachment attachment;
	private NodeAttachment nodeAttachment;
	private AttachmentInstance attachmentInstance;
	private NodeAttachmentInstance noodeAttachmentInstance;
	
	private String nextWorkflowNodeId = "";
	private String workflowInstanceId;
	private String workInstanceId;
	private String attachmentId = "";
	private String nodeAttachmentId = "";
	private String paramValue = "";
	private String nodeParamValue = "";
	private String nodeInstanceId;
	
	private ResourceService resourceService;
	private WorklistService worklistService;
	private WorkflowService workflowService;
	private WorkflowNodeService workflowNodeService;
	private WorkflowPathService workflowPathService;
	private WorkflowInstanceService workflowInstanceService;
	private ContextInstanceService contextInstanceService;
	private NodeContextInstanceService nodeContextInstanceService;
	private WorkflowNodeInstanceService workflowNodeInstanceService;
	private NodeTemplateService nodeTemplateService;
	private AttachmentService attachmentService;
	private NodeAttachmentService nodeAttachmentService;
	private NodeAttachmentInstanceService nodeAttachmentInstanceService;
	private AttachmentInstanceService attachmentInstanceService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_wfWorklistId;
	private String query_wfWorkhistoryId;
	private String query_handlers;
	private String query_status;
	private Long nodeId;
	private Long instanceId;
	private String contextId;
	private String nodeContextId;
	private String work_template;


	public void prepare() {
		super.prepare();

//		if (this.selectedId != null && !this.selectedId.equals("")) {
//			this.worklist = worklistService.getWorklist(Long
//					.parseLong(selectedId));
//		}
	}
	public void getObjectForPage()
	{
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
		workflow = workflowService.getWorkflow(workflowInstance.getWorkflowId());
		workflowNode = workflowNodeService.getWorkflowNode(workflowInstance.getWorkflowNodeId());
		workflowNodeInstanceMapList = workflowService.getNodeInstanceList(workflowInstance.getWorkflowInstanceId());
		nodeList = workflowService.getNextNodeList(workflowInstance.getWorkflowNodeId());
		contextList = worklistService.getContextByNodeInstance(nodeId);
		nodeContextList = worklistService.getNodeContextByNodeInstance(nodeId);
		attachmentList = worklistService.getAttachmentByNodeInstance(nodeId);
		nodeAttachmentList = worklistService.getNodeAttachmentByNodeInstance(nodeId);
	}
	

	public String showWorkList() {
		Map queryMap = new HashMap();
		queryMap.put("userName", loginUser.getName());
		queryMap.put("wfWorkhistoryId", query_wfWorkhistoryId);
		queryMap.put("handlers", query_handlers);
		queryMap.put("status", query_status);

		workList = worklistService.getWorklistByUserName(loginUser.getName());
		// workhistoryList = worklistService.getWorklistByUserName(queryMap);
		
		

		return RESULT_QUERY;
	}

	public String showWorkDetail() {
		nodeId = new Long(selectedId);
		log.error("nodeId==="+nodeId);
		
		workflowNodeInstance = workflowNodeInstanceService.getWorkflowNodeInstance(nodeId);
		instanceId = workflowNodeInstance.getWorkflowInstanceId();
		
		getObjectForPage();
        WorkPanelGenerator gene = new WorkPanelGenerator();
		
		gene.setObject("userId", "jczhou" + System.currentTimeMillis());
		gene.setObject("workflowNodeInstanceMapList", workflowNodeInstanceMapList);
		
		work_template = gene.construct(nodeTemplateService.getNodeTemplate(1l).getTemplate());		
		log.error("^^^^^^ showWorkDetail work_template new :" + work_template);

		return RESULT_DETAIL;
	}
	
	public String showWorkWatchDetail() {
		workflowPath = workflowPathService.getWorkflowPath(Long.parseLong(nextWorkflowNodeId));
		getObjectForPage();
		return RESULT_WATCH;
	}
	
	public String goNext() {
		log.error("instanceid="+workflowInstanceId);
		log.error("pathid="+nextWorkflowNodeId);
		instanceId = new Long(workflowInstanceId);
		workflowPath = workflowPathService.getWorkflowPath(Long.parseLong(nextWorkflowNodeId));
		log.error("nodefrom="+workflowPath.getNodeFrom());
		try {

			workflowService.saveInstanceToNext(instanceId,workflowPath.getNodeTo());

			message = "工作流操作成功!";

		} catch (Exception e) {
			e.printStackTrace();
			message = "工作流操作失败!";
		}
		List<Map> queryList = workflowNodeInstanceService.getNodeInstanceId(instanceId, workflowPath.getNodeFrom());
		if (queryList != null)
		{ 
			Map tempMap = (Map) queryList.get(0);
			nodeId = Long.parseLong(tempMap.get("NODE_INSTANCE_ID").toString());
		}
		log.error("nodeID="+nodeId);
		getObjectForPage();
		WorkPanelGenerator gene = new WorkPanelGenerator();
		
		gene.setObject("userId", "jczhou" + System.currentTimeMillis());
		
		work_template = gene.construct(nodeTemplateService.getNodeTemplate(1l).getTemplate());		
		log.error("^^^^^^ goNext work_template new :" + work_template);
		
	
		return RESULT_WATCH;
	}
	
	public String saveInstanceContext()
	{
		instanceId = new Long(workflowInstanceId);
		Long cId = new Long(contextId);
		log.error("instanceId="+instanceId);
		log.error("cId=="+cId);
		log.error("paramValue===="+paramValue);
		Long version = null;
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
		List<Map> queryList = workflowNodeInstanceService.getNodeInstanceId(instanceId, workflowInstance.getWorkflowNodeId());
		if (queryList != null)
		{ 
			Map tempMap = (Map) queryList.get(0);
			nodeId = Long.parseLong(tempMap.get("NODE_INSTANCE_ID").toString());
		}
		List<Map> versionList = contextInstanceService.getContextVersion(instanceId,cId);
		if (versionList != null)
		{ 
			Map tempMap = (Map) versionList.get(0);
			if (tempMap.get("VERSION") != null) {
				version = Long.parseLong(tempMap.get("VERSION").toString())+1l;
			}
			else {
				version = 1l;
			}
		}
		log.error("version===="+version);
		ContextInstance contextInstance = new ContextInstance();
		contextInstance.setContextId(cId);
		contextInstance.setParamValue(paramValue.replaceAll(",","").trim());
		contextInstance.setWorkflowInstanceId(instanceId);
		contextInstance.setWorkflowNodeInstanceId(nodeId);
		contextInstance.setVersion(version);
		contextInstance.setCreateTime(new Date());
		contextInstance.setCreateUser(loginUser.getUserId());
		contextInstanceService.saveContextInstance(contextInstance);
		paramValue = null;
		nodeParamValue = null;
		workflowNodeInstance = workflowNodeInstanceService.getWorkflowNodeInstance(nodeId);
		getObjectForPage();
        WorkPanelGenerator gene = new WorkPanelGenerator();
		
		gene.setObject("userId", "jczhou" + System.currentTimeMillis());
		gene.setObject("workflowNodeInstanceMapList", workflowNodeInstanceMapList);
		
		work_template = gene.construct(nodeTemplateService.getNodeTemplate(1l).getTemplate());	
		return RESULT_DETAIL;
	}
	
	public String saveNodeInstanceContext()
	{
		instanceId = new Long(workflowInstanceId);
		//nodeId = new Long(nodeInstanceId);
		Long cId = new Long(nodeContextId);
		log.error("InstanceId="+instanceId);
		log.error("cId=="+cId);
		log.error("nodeParamValue===="+nodeParamValue);
		Long version = null;
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
		List<Map> queryList = workflowNodeInstanceService.getNodeInstanceId(instanceId, workflowInstance.getWorkflowNodeId());
		if (queryList != null)
		{ 
			Map tempMap = (Map) queryList.get(0);
			nodeId = Long.parseLong(tempMap.get("NODE_INSTANCE_ID").toString());
		}
		List<Map> versionList = nodeContextInstanceService.getNodeContextVersion(nodeId,cId);
		if (versionList != null)
		{ 
			Map tempMap = (Map) versionList.get(0);
			if (tempMap.get("VERSION") != null) {
				version = Long.parseLong(tempMap.get("VERSION").toString())+1l;
			}
			else {
				version = 1l;
			}
		}
		log.error("version===="+version);
		NodeContextInstance nodeContextInstance = new NodeContextInstance();
		nodeContextInstance.setNodeContextId(cId);
		nodeContextInstance.setParamValue(nodeParamValue.replaceAll(",","").trim());
		nodeContextInstance.setWorkflowNodeInstanceId(nodeId);
		nodeContextInstance.setVersion(version);
		nodeContextInstance.setCreateTime(new Date());
		nodeContextInstance.setCreateUser(loginUser.getUserId());
		
		nodeContextInstanceService.saveNodeContextInstance(nodeContextInstance);
		paramValue = null;
		nodeParamValue = null;
		
		workflowNodeInstance = workflowNodeInstanceService.getWorkflowNodeInstance(nodeId);
		getObjectForPage();
        WorkPanelGenerator gene = new WorkPanelGenerator();
		
		gene.setObject("userId", "jczhou" + System.currentTimeMillis());
		gene.setObject("workflowNodeInstanceMapList", workflowNodeInstanceMapList);
		
		work_template = gene.construct(nodeTemplateService.getNodeTemplate(1l).getTemplate());	
		return RESULT_DETAIL;
	}	
	
	public String deleteAttachment(){
		List deleteList = new ArrayList();
		instanceId = new Long(workflowInstanceId);
		deleteList.add(new Long(attachmentId));
		attachmentService.delete(deleteList);
		paramValue = null;
		nodeParamValue = null;
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
		List<Map> queryList = workflowNodeInstanceService.getNodeInstanceId(instanceId, workflowInstance.getWorkflowNodeId());
		if (queryList != null)
		{ 
			Map tempMap = (Map) queryList.get(0);
			nodeId = Long.parseLong(tempMap.get("NODE_INSTANCE_ID").toString());
		}
		getObjectForPage();
		WorkPanelGenerator gene = new WorkPanelGenerator();
		gene.setObject("userId", "jczhou" + System.currentTimeMillis());
		gene.setObject("workflowNodeInstanceMapList", workflowNodeInstanceMapList);
		work_template = gene.construct(nodeTemplateService.getNodeTemplate(1l).getTemplate());	
		return RESULT_DETAIL;
	}
	
	public String deleteNodeAttachment(){
		List deleteList = new ArrayList();
		instanceId = new Long(workflowInstanceId);
		deleteList.add(new Long(nodeAttachmentId));
		nodeAttachmentService.delete(deleteList);
		paramValue = null;
		nodeParamValue = null;
		getObjectForPage();
		WorkPanelGenerator gene = new WorkPanelGenerator();
		gene.setObject("userId", "jczhou" + System.currentTimeMillis());
		gene.setObject("workflowNodeInstanceMapList", workflowNodeInstanceMapList);
		work_template = gene.construct(nodeTemplateService.getNodeTemplate(1l).getTemplate());
		return RESULT_DETAIL;
	}
	public String toAddAttachment()
	{
		instanceId = new Long(workflowInstanceId);
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
		List<Map> queryList = workflowNodeInstanceService.getNodeInstanceId(instanceId, workflowInstance.getWorkflowNodeId());
		if (queryList != null)
		{ 
			Map tempMap = (Map) queryList.get(0);
			nodeId = Long.parseLong(tempMap.get("NODE_INSTANCE_ID").toString());
		}
		workflow = workflowService.getWorkflow(workflowInstance.getWorkflowId());
		workflowNodeInstance = workflowNodeInstanceService.getWorkflowNodeInstance(nodeId);
		attachment = new Attachment();
		attachment.setWorkflowId(workflowInstance.getWorkflowId());
		return RESULT_ATTACHMENT_ADD;
	}
	public String saveAttachment()
	{
		instanceId = new Long(workInstanceId);
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
		attachment.setWorkflowId(workflowInstance.getWorkflowId());
		attachment.setCreateTime(new Date());
		attachment.setCreateUser(loginUser.getUserId());
		try {
			
			attachmentService.saveAttachment(attachment);

			message = "工作流附件新增成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message = "工作流附件新增失败!";
		}

		addActionMessage(message);
		List<Map> queryList = workflowNodeInstanceService.getNodeInstanceId(instanceId, workflowInstance.getWorkflowNodeId());
		if (queryList != null)
		{ 
			Map tempMap = (Map) queryList.get(0);
			nodeId = Long.parseLong(tempMap.get("NODE_INSTANCE_ID").toString());
		}
		getObjectForPage();
        WorkPanelGenerator gene = new WorkPanelGenerator();
		
		gene.setObject("userId", "jczhou" + System.currentTimeMillis());
		gene.setObject("workflowNodeInstanceMapList", workflowNodeInstanceMapList);
		
		work_template = gene.construct(nodeTemplateService.getNodeTemplate(1l).getTemplate());
		return RESULT_DETAIL;
	}
	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		worklistService.saveWorklist(worklist);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (worklist.getHandlers().length() == 0) {
			this.addActionError("** Handlers 不能为空 !");
		}
		if (worklist.getStatus().length() == 0) {
			this.addActionError("** Status 不能为空 !");
		}

		this.setValidatorResult("/wf/WorklistAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		worklist = worklistService.getWorklist(id);

		return RESULT_EDIT;
	}

	public String update() {

		worklistService.updateWorklist(worklist);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (worklist.getHandlers().length() == 0) {
			this.addActionError("** Handlers 不能为空 !");
		}
		if (worklist.getStatus().length() == 0) {
			this.addActionError("** Status 不能为空 !");
		}
		this.setValidatorResult("/wf/WorklistUpdate.jsp");
	}
	public String toUploadAttachmentInstance(){
		instanceId = new Long(workflowInstanceId);
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
		attachment = attachmentService.getAttachment(new Long(attachmentId));
		return RESULT_TO_UPLOAD;
	}
	public String toUploadNodeAttachmentInstance(){
		instanceId = new Long(workflowInstanceId);
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
		nodeAttachment = nodeAttachmentService.getNodeAttachment(new Long(nodeAttachmentId));
		return RESULT_TO_NODE_UPLOAD;
	}
	public String uploadAttachmentInstanceFile() throws FileNotFoundException, IOException{
		instanceId = new Long(workflowInstanceId);
		try {   
            InputStream in = new FileInputStream(file); 
            String dir = constants.get("DIR").toString(); 
            log.error("dir===="+dir);
            String originalFileName = this.getFileFileName().toString(); //原始文件名
            String type = originalFileName.substring(originalFileName.lastIndexOf(".")+1); //文件类型
            String d = new Timestamp(System.currentTimeMillis()).toString();
            String finalFileName = d.substring(0,10).replaceAll("-","")+d.substring(11,19).replaceAll(":","")+d.substring(20);
            File uploadFile = new File(dir,finalFileName+"."+type);   
            OutputStream out = new FileOutputStream(uploadFile);   
            byte[] buffer = new byte[1024 * 1024];   
            int length;   
            while ((length = in.read(buffer)) > 0) {   
                out.write(buffer, 0, length);   
            } 
            log.error("fname===="+finalFileName);
            //添加resource表的记录
            Resource resource = new Resource();
            resource.setCreateTime(new Date());
            resource.setName(originalFileName.substring(0,originalFileName.lastIndexOf(".")));
            resource.setType(type);
            resource.setDir(dir+finalFileName+"."+type);
            resourceService.saveResource(resource);
            
            Long version = null;
            List<Map> versionList = attachmentInstanceService.getAttachmentInstanceVersion(instanceId,new Long(attachmentId));
            if (versionList != null)
    		{ 
    			Map tempMap = (Map) versionList.get(0);
    			if (tempMap.get("VERSION") != null) {
    				version = Long.parseLong(tempMap.get("VERSION").toString())+1l;
    			}
    			else {
    				version = 1l;
    			}
    		}else{
    			version = 1l;
    		}
    		log.error("version===="+version);
          //更新attachmentInstance表的记录
            AttachmentInstance attachmentInstance = new AttachmentInstance();
            attachmentInstance.setResourceId(resource.getResourceId());
            attachmentInstance.setAttachmentId(new Long(attachmentId));
            attachmentInstance.setVersion(version);
            attachmentInstance.setWorkflowInstanceId(instanceId);
            attachmentInstanceService.saveAttachmentInstance(attachmentInstance);
            in.close();   
            out.close();   
        } catch (FileNotFoundException ex) {   
            ex.printStackTrace();   
        } catch (IOException ex) {   
            ex.printStackTrace();   
        } 
        workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
        List<Map> queryList = workflowNodeInstanceService.getNodeInstanceId(instanceId, workflowInstance.getWorkflowNodeId());
		if (queryList != null)
		{ 
			Map tempMap = (Map) queryList.get(0);
			nodeId = Long.parseLong(tempMap.get("NODE_INSTANCE_ID").toString());
		}
        getObjectForPage();
        return RESULT_DETAIL;
	}
	
	public String uploadNodeAttachmentInstanceFile() throws FileNotFoundException, IOException{
		instanceId = new Long(workflowInstanceId);
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
        List<Map> queryList = workflowNodeInstanceService.getNodeInstanceId(instanceId, workflowInstance.getWorkflowNodeId());
		if (queryList != null)
		{ 
			Map tempMap = (Map) queryList.get(0);
			nodeId = Long.parseLong(tempMap.get("NODE_INSTANCE_ID").toString());
		}
		try {   
            InputStream in = new FileInputStream(file); 
            String dir = constants.get("DIR").toString(); 
            log.error("dir===="+dir);
            String originalFileName = this.getFileFileName().toString(); //原始文件名
            String type = originalFileName.substring(originalFileName.lastIndexOf(".")+1); //文件类型
            String d = new Timestamp(System.currentTimeMillis()).toString();
            String finalFileName = d.substring(0,10).replaceAll("-","")+d.substring(11,19).replaceAll(":","")+d.substring(20);
            File uploadFile = new File(dir,finalFileName+"."+type);   
            OutputStream out = new FileOutputStream(uploadFile);   
            byte[] buffer = new byte[1024 * 1024];   
            int length;   
            while ((length = in.read(buffer)) > 0) {   
                out.write(buffer, 0, length);   
            } 
            log.error("fname===="+finalFileName);
            //添加resource表的记录
            Resource resource = new Resource();
            resource.setCreateTime(new Date());
            resource.setName(originalFileName.substring(0,originalFileName.lastIndexOf(".")));
            resource.setType(type);
            resource.setDir(dir+finalFileName+"."+type);
            resourceService.saveResource(resource);
            
            Long version = null;
            List<Map> versionList = nodeAttachmentInstanceService.getNodeAttachmentInstanceVersion(nodeId,new Long(nodeAttachmentId));
            if (versionList != null)
    		{ 
    			Map tempMap = (Map) versionList.get(0);
    			if (tempMap.get("VERSION") != null) {
    				version = Long.parseLong(tempMap.get("VERSION").toString())+1l;
    			}
    			else {
    				version = 1l;
    			}
    		}else{
    			version = 1l;
    		}
    		log.error("version===="+version);
          //更新attachmentInstance表的记录
            NodeAttachmentInstance nodeAttachmentInstance = new NodeAttachmentInstance();
            nodeAttachmentInstance.setResourceId(resource.getResourceId());
            nodeAttachmentInstance.setNodeAttachmentId(new Long(nodeAttachmentId));
            nodeAttachmentInstance.setVersion(version);
            nodeAttachmentInstance.setWorkflowNodeInstanceId(nodeId);
            nodeAttachmentInstanceService.saveNodeAttachmentInstance(nodeAttachmentInstance);
            in.close();   
            out.close();   
        } catch (FileNotFoundException ex) {   
            ex.printStackTrace();   
        } catch (IOException ex) {   
            ex.printStackTrace();   
        } 
        getObjectForPage();
        return RESULT_DETAIL;
	}
	public String searchNodeAttachmentHis(){
		instanceId = new Long(workflowInstanceId);
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
		nodeAttachment = nodeAttachmentService.getNodeAttachment(new Long(nodeAttachmentId));
		nodeAttachmentList = nodeAttachmentInstanceService.getNodeAttachmentInstanceHis(new Long(nodeAttachmentId));
		return RESULT_NODE_HIS;
	}
	public String searchAttachmentHis(){
		instanceId = new Long(workflowInstanceId);
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
		attachment = attachmentService.getAttachment(new Long(attachmentId));
		attachmentList = attachmentInstanceService.getAttachmentInstanceHis(new Long(attachmentId));
		return RESULT_HIS;
	}
	public String returnDetail(){
		instanceId = new Long(workflowInstanceId);
		workflowInstance = workflowInstanceService.getWorkflowInstance(instanceId);
        List<Map> queryList = workflowNodeInstanceService.getNodeInstanceId(instanceId, workflowInstance.getWorkflowNodeId());
		if (queryList != null)
		{ 
			Map tempMap = (Map) queryList.get(0);
			nodeId = Long.parseLong(tempMap.get("NODE_INSTANCE_ID").toString());
		}
		getObjectForPage();
        WorkPanelGenerator gene = new WorkPanelGenerator();
		
		gene.setObject("userId", "jczhou" + System.currentTimeMillis());
		gene.setObject("workflowNodeInstanceMapList", workflowNodeInstanceMapList);
		
		work_template = gene.construct(nodeTemplateService.getNodeTemplate(1l).getTemplate());		
		log.error("^^^^^^ showWorkDetail work_template new :" + work_template);

		return RESULT_DETAIL;
	}
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	
	public void setWorklist(Worklist worklist) {
		this.worklist = worklist;
	}

	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getWorkflowInstanceId() {
		return workflowInstanceId;
	}

	public void setWorkflowInstanceId(String workflowInstanceId) {
		this.workflowInstanceId = workflowInstanceId;
	}

	public String getNextWorkflowNodeId() {
		return nextWorkflowNodeId;
	}

	public void setNextWorkflowNodeId(String nextWorkflowNodeId) {
		this.nextWorkflowNodeId = nextWorkflowNodeId;
	}

	public Worklist getWorklist() {
		return worklist;
	}
	public void setWorklistService(WorklistService worklistService) {
		this.worklistService = worklistService;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_wfWorklistId() {
		return this.query_wfWorklistId;
	}

	public void setQuery_wfWorklistId(String wfWorklistId) {
		this.query_wfWorklistId = wfWorklistId;
	}

	public String getQuery_wfWorkhistoryId() {
		return this.query_wfWorkhistoryId;
	}

	public void setQuery_wfWorkhistoryId(String wfWorkhistoryId) {
		this.query_wfWorkhistoryId = wfWorkhistoryId;
	}

	public String getQuery_handlers() {
		return this.query_handlers;
	}

	public void setQuery_handlers(String handlers) {
		this.query_handlers = handlers;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	public List<Map> getWorkList() {
		return workList;
	}

	public void setWorkList(List<Map> workList) {
		this.workList = workList;
	}

	public List<Map> getWorkhistoryList() {
		return workhistoryList;
	}

	public void setWorkhistoryList(List<Map> workhistoryList) {
		this.workhistoryList = workhistoryList;
	}

	public WorklistService getWorklistService() {
		return worklistService;
	}



	public List<Map> getNodeList() {
		return nodeList;
	}



	public void setNodeList(List<Map> nodeList) {
		this.nodeList = nodeList;
	}



	public List<Map> getWorkflowNodeInstanceMapList() {
		return workflowNodeInstanceMapList;
	}



	public void setWorkflowNodeInstanceMapList(List<Map> workflowNodeInstanceMapList) {
		this.workflowNodeInstanceMapList = workflowNodeInstanceMapList;
	}
	public Workflow getWorkflow() {
		return workflow;
	}
	public void setWorkflow(Workflow workflow) {
		this.workflow = workflow;
	}
	public WorkflowNode getWorkflowNode() {
		return workflowNode;
	}
	public void setWorkflowNode(WorkflowNode workflowNode) {
		this.workflowNode = workflowNode;
	}
	public WorkflowInstance getWorkflowInstance() {
		return workflowInstance;
	}
	public void setWorkflowInstance(WorkflowInstance workflowInstance) {
		this.workflowInstance = workflowInstance;
	}
	public WorkflowService getWorkflowService() {
		return workflowService;
	}
	public void setWorkflowService(WorkflowService workflowService) {
		this.workflowService = workflowService;
	}
	public WorkflowNodeService getWorkflowNodeService() {
		return workflowNodeService;
	}
	public void setWorkflowNodeService(WorkflowNodeService workflowNodeService) {
		this.workflowNodeService = workflowNodeService;
	}
	public WorkflowInstanceService getWorkflowInstanceService() {
		return workflowInstanceService;
	}
	public void setWorkflowInstanceService(
			WorkflowInstanceService workflowInstanceService) {
		this.workflowInstanceService = workflowInstanceService;
	}
	public String getWork_template() {
		return work_template;
	}
	public void setWork_template(String workTemplate) {
		work_template = workTemplate;
	}
	public NodeTemplateService getNodeTemplateService() {
		return nodeTemplateService;
	}
	public void setNodeTemplateService(NodeTemplateService nodeTemplateService) {
		this.nodeTemplateService = nodeTemplateService;
	}
	public WorkflowNodeInstance getWorkflowNodeInstance() {
		return workflowNodeInstance;
	}
	public void setWorkflowNodeInstance(WorkflowNodeInstance workflowNodeInstance) {
		this.workflowNodeInstance = workflowNodeInstance;
	}
	public WorkflowNodeInstanceService getWorkflowNodeInstanceService() {
		return workflowNodeInstanceService;
	}
	public void setWorkflowNodeInstanceService(
			WorkflowNodeInstanceService workflowNodeInstanceService) {
		this.workflowNodeInstanceService = workflowNodeInstanceService;
	}
	public Context getContext() {
		return context;
	}
	public void setContext(Context context) {
		this.context = context;
	}
	public NodeContext getNodeContext() {
		return nodeContext;
	}
	public void setNodeContext(NodeContext nodeContext) {
		this.nodeContext = nodeContext;
	}
	public List<Map> getContextList() {
		return contextList;
	}
	public void setContextList(List<Map> contextList) {
		this.contextList = contextList;
	}
	public List<Map> getNodeContextList() {
		return nodeContextList;
	}
	public void setNodeContextList(List<Map> nodeContextList) {
		this.nodeContextList = nodeContextList;
	}
	public WorkflowPath getWorkflowPath() {
		return workflowPath;
	}
	public void setWorkflowPath(WorkflowPath workflowPath) {
		this.workflowPath = workflowPath;
	}
	public WorkflowPathService getWorkflowPathService() {
		return workflowPathService;
	}
	public void setWorkflowPathService(WorkflowPathService workflowPathService) {
		this.workflowPathService = workflowPathService;
	}
	public String getContextId() {
		return contextId;
	}
	public void setContextId(String contextId) {
		this.contextId = contextId;
	}
	public ContextInstance getContextInstance() {
		return contextInstance;
	}
	public void setContextInstance(ContextInstance contextInstance) {
		this.contextInstance = contextInstance;
	}
	public String getParamValue() {
		return paramValue;
	}
	public void setParamValue(String paramValue) {
		this.paramValue = paramValue;
	}
	public ContextInstanceService getContextInstanceService() {
		return contextInstanceService;
	}
	public void setContextInstanceService(
			ContextInstanceService contextInstanceService) {
		this.contextInstanceService = contextInstanceService;
	}
	public String getNodeParamValue() {
		return nodeParamValue;
	}
	public void setNodeParamValue(String nodeParamValue) {
		this.nodeParamValue = nodeParamValue;
	}
	public String getNodeInstanceId() {
		return nodeInstanceId;
	}
	public void setNodeInstanceId(String nodeInstanceId) {
		this.nodeInstanceId = nodeInstanceId;
	}
	public String getNodeContextId() {
		return nodeContextId;
	}
	public void setNodeContextId(String nodeContextId) {
		this.nodeContextId = nodeContextId;
	}
	public NodeContextInstance getNodeContextInstance() {
		return nodeContextInstance;
	}
	public void setNodeContextInstance(NodeContextInstance nodeContextInstance) {
		this.nodeContextInstance = nodeContextInstance;
	}
	public NodeContextInstanceService getNodeContextInstanceService() {
		return nodeContextInstanceService;
	}
	public void setNodeContextInstanceService(
			NodeContextInstanceService nodeContextInstanceService) {
		this.nodeContextInstanceService = nodeContextInstanceService;
	}
	public List<Map> getAttachmentList() {
		return attachmentList;
	}
	public void setAttachmentList(List<Map> attachmentList) {
		this.attachmentList = attachmentList;
	}
	public List<Map> getNodeAttachmentList() {
		return nodeAttachmentList;
	}
	public void setNodeAttachmentList(List<Map> nodeAttachmentList) {
		this.nodeAttachmentList = nodeAttachmentList;
	}
	public Attachment getAttachment() {
		return attachment;
	}
	public void setAttachment(Attachment attachment) {
		this.attachment = attachment;
	}
	public NodeAttachment getNodeAttachment() {
		return nodeAttachment;
	}
	public void setNodeAttachment(NodeAttachment nodeAttachment) {
		this.nodeAttachment = nodeAttachment;
	}
	public String getAttachmentId() {
		return attachmentId;
	}
	public void setAttachmentId(String attachmentId) {
		this.attachmentId = attachmentId;
	}
	public String getNodeAttachmentId() {
		return nodeAttachmentId;
	}
	public void setNodeAttachmentId(String nodeAttachmentId) {
		this.nodeAttachmentId = nodeAttachmentId;
	}
	public AttachmentService getAttachmentService() {
		return attachmentService;
	}
	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}
	public NodeAttachmentService getNodeAttachmentService() {
		return nodeAttachmentService;
	}
	public void setNodeAttachmentService(NodeAttachmentService nodeAttachmentService) {
		this.nodeAttachmentService = nodeAttachmentService;
	}
	public String getWorkInstanceId() {
		return workInstanceId;
	}
	public void setWorkInstanceId(String workInstanceId) {
		this.workInstanceId = workInstanceId;
	}
	public AttachmentInstance getAttachmentInstance() {
		return attachmentInstance;
	}
	public void setAttachmentInstance(AttachmentInstance attachmentInstance) {
		this.attachmentInstance = attachmentInstance;
	}
	public NodeAttachmentInstance getNoodeAttachmentInstance() {
		return noodeAttachmentInstance;
	}
	public void setNoodeAttachmentInstance(
			NodeAttachmentInstance noodeAttachmentInstance) {
		this.noodeAttachmentInstance = noodeAttachmentInstance;
	}
	public ResourceService getResourceService() {
		return resourceService;
	}
	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}
	public NodeAttachmentInstanceService getNodeAttachmentInstanceService() {
		return nodeAttachmentInstanceService;
	}
	public void setNodeAttachmentInstanceService(
			NodeAttachmentInstanceService nodeAttachmentInstanceService) {
		this.nodeAttachmentInstanceService = nodeAttachmentInstanceService;
	}
	public AttachmentInstanceService getAttachmentInstanceService() {
		return attachmentInstanceService;
	}
	public void setAttachmentInstanceService(
			AttachmentInstanceService attachmentInstanceService) {
		this.attachmentInstanceService = attachmentInstanceService;
	}
	
}
